package com.william.jdbcplus.core.dialect.generator;

import com.william.jdbcplus.common.enums.DbType;
import com.william.jdbcplus.core.dialect.generator.impl.BaseAutoKey;
import com.william.jdbcplus.core.dialect.generator.impl.OracleAutoKey;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 表主键生成器工厂
 *
 * @author william
 */
public class AutoKeyFactory {

    /**
     * 储存反射类表信息
     */
    private static final Map<DbType, IAutoKey> KEY_INFO_CACHE = new ConcurrentHashMap<>();

    static {
        for (DbType dbType : DbType.values()) {
            KEY_INFO_CACHE.put(dbType, initKey(dbType));
        }
    }

    /**
     * @param dbType 数据库类型
     */
    private static IAutoKey initKey(DbType dbType) {
        switch (dbType) {
            case MYSQL:
                return new BaseAutoKey();
            case MARIADB:
                return new BaseAutoKey();
            case ORACLE:
                return new OracleAutoKey();
            case DB2:
                return new BaseAutoKey();
            case H2:
                return new BaseAutoKey();
            case SQL_SERVER:
                return new BaseAutoKey();
            case SQL_SERVER2005:
                return new BaseAutoKey();
            case POSTGRE_SQL:
                return new BaseAutoKey();
            case HSQL:
                return new BaseAutoKey();
            case SQLITE:
                return new BaseAutoKey();
            case DM:
                return new BaseAutoKey();
            default:
                return new BaseAutoKey();
        }
    }

    public static IAutoKey getAutoKey(DbType dbType) {
        IAutoKey info = KEY_INFO_CACHE.get(dbType);
        if (null != info) {
            return info;
        } else {
            return null;
        }
    }
}
